<?php

defined('SYSPATH') or die('No direct script access.');

class HTML extends Kohana_HTML {

    /**
     * Creates a script link.
     *
     *     echo HTML::script('media/js/jquery.min.js');
     *
     * @param   string   file name
     * @param   array    default attributes
     * @param   boolean  include the index page
     * @param   boolean     App Si esta dentro de un modulo
     * @return  string
     * @uses    URL::base
     * @uses    HTML::attributes
     */
    public static function script($file, array $attributes = NULL, $index = FALSE, $app = FALSE) {
        if (is_array($file)) {
            $out = '';
            foreach ($file as $f) {
                $out .= HTML::script($f, $attributes, $index, $app);
            }
            return $out;
        }
        if (strpos($file, '://') === FALSE) {
            // Add the base URL
            $path = ($app) ? '' : 'resources/js/';
            $file = URL::base($index) . $path . $file;

            // si no tiene extencion se la agrego
            if (!strstr($file, '.js')) {
                $file .= '.js';
            }
        }
        // Set the script link
        $attributes['src'] = $file;
        return '<script' . HTML::attributes($attributes) . '></script>';
    }

    /**
     * Creates a style sheet link element.
     *
     *     echo HTML::style(array);
     *
     * @param   string      file name
     * @param   array       default attributes
     * @param   boolean     include the index page
     * @param   boolean     App Si esta dentro de un modulo
     * @return  string
     * @uses    URL::base
     * @uses    HTML::attributes
     */
    public static function style($file, array $attributes = NULL, $index = FALSE, $app = FALSE) {
        if (is_array($file)) {
            $out = '';
            foreach ($file as $f) {
                $out .= HTML::style($f, $attributes, $index, $app);
            }
            return $out;
        }
        if (strpos($file, '://') === FALSE) {
            // Add the base URL
            $path = ($app) ? '' : 'resources/css/';
            $file = URL::base($index) . $path . $file;

            // si no tiene extencion se la agrego
            if (!strstr($file, '.css')) {
                $file .= '.css';
            }
        }
        // Set the stylesheet link
        $attributes['href'] = $file;
        // Set the stylesheet rel
        $attributes['rel'] = 'stylesheet';
        // Set the stylesheet type
        $attributes['type'] = 'text/css';
        return '<link' . HTML::attributes($attributes) . ' />';
    }

    /**
     * Creates a script tags.
     *
     * echo HTML::raw_js('var lang = "es";');
     *
     * @param   string   script string
     * @return  string
     * @uses    URL::base
     */
    public static function raw_js($string) {
        return '<script>' . $string . '</script>';
    }

    /**
     * Creates a style tags.
     *
     * echo HTML::raw_css('a{text-decoration:none;}');
     *
     * @param   string   script string
     * @return  string
     * @uses    URL::base
     */
    public static function raw_css($string) {
        return '<style>' . $string . '</style>';
    }

}